var gulp = require("gulp"),
	rename = require("gulp-rename"),
	sass = require("gulp-sass"),
	// runSequence = require("run-sequence"),
	//rev = require("gulp-rev"),
	//revCollector = require("gulp-rev-collector"),
	//livereload = require("gulp-livereload"),
	connect = require("gulp-connect"),
	//uglify = require("gulp-uglify"),
	//ug = require("gulp-ug"),
	gulpSequence = require("gulp-sequence"),
	clean = require("gulp-clean"),
	useref = require("gulp-useref"),
	//concat = require("gulp-concat"),
	// pump = require("pump"),
	// cleanCSS = require("gulp-clean-css"),
	fileinclude = require("gulp-file-include"),
	sourcemaps = require("gulp-sourcemaps"),
	//babel = require("gulp-babel"),
	ts = require("gulp-typescript"),
	tsProject = ts.createProject("tsconfig.json"),
	plumber = require("gulp-plumber"),
	del = require("del");

var paths = {
	css:["src/**/*.css"],
	js:["./src/js/*.js"],
	jssize:["./src/ts/*.ts"],
	html:["./src/template/*.html"]
};

gulp.task("connect", function() {
	connect.server({
		root:"./src/app",
		port: 9999,
		livereload: true,
		host: "0.0.0.0"
	});
});	

//清空目标文件
gulp.task("cleanDst", function () {
	return gulp.src(["dist","rev"], {read: false})
		.pipe(clean());
});

gulp.task("css", function() {
	return gulp.src("./css/*.css")
		.pipe(connect.reload());
});


gulp.task("js", function() {
	return gulp.src(paths.js)
		.pipe(connect.reload());
});

gulp.task("jssize", function() {
	return gulp.src(tsProject.config.include)
		.pipe(plumber())
		.pipe(tsProject())
		.pipe(rename({suffix:".min"}))
		// .pipe(uglify())
		// .pipe(ug())
		.pipe(gulp.dest("./src/app/static/js"));
});

//清除旧文件
gulp.task("clean:dist",function(cb){
	return del([
		"./dist/**/*"
	],cb); 
});

gulp.task("useref",function () {       
	return gulp.src("./src/app/*.html")       
		.pipe(useref())                    
		.pipe(gulp.dest("./dist"));                            
});

gulp.task("html", function() {
	return gulp.src(paths.html)
		.pipe(fileinclude({
			prefix: "@@",//变量前缀 @@include
			basepath: "./src/int",//引用文件路径
			indent:true//保留文件的缩进
		}))
		.pipe(connect.reload())
		.pipe(gulp.dest("./src/app"));
});

//复制图片文件
gulp.task("copy-image",function(){
	return gulp.src("./src/app/static/images/**/*")
		.pipe(gulp.dest("./dist/static/images"));
});
//复制js文件
gulp.task("copy-js",function(){
	return gulp.src("./src/app/static/js/**/*")
		.pipe(gulp.dest("./dist/static/js"));
});

gulp.task("copy-libs",function(){
	return gulp.src("./src/app/static/libs/**/*")
		.pipe(gulp.dest("./dist/static/libs"));
});

//复制css文件
gulp.task("copy-css",function(){
	return gulp.src("./src/app/static/css/**/*")
		.pipe(gulp.dest("./dist/static/css"));
});

gulp.task("sass",function() {
	return gulp.src("./sass/default.scss")
		.pipe(sourcemaps.init({loadMaps: true}))
		.pipe(sass({outputStyle:"compressed"}).on("error",sass.logError))
		.pipe(sourcemaps.write("maps"))
		// .pipe(rev())
		// .pipe(rev.manifest())
		.pipe(gulp.dest("./src/app/static/css"))
		.pipe(connect.reload());
});

//生产环境
gulp.task("build",gulpSequence("clean:dist","useref",["copy-image","copy-css","copy-js","copy-libs"]));

gulp.task("watch", function() {
	// livereload.listen(); //要在这里调用listen()方法
	gulp.watch("./sass/*.scss", ["sass"]);
	gulp.watch(paths.html, ["html"]);
	// gulp.watch("./src/app/*.html", ["useref"]);
	// gulp.watch(paths.js, ['js']);
	gulp.watch(paths.jssize, ["jssize"]);
});

gulp.task("default", ["connect","clean:dist","watch"]);
